<?php

namespace app\plugins\mobile\forms;

use luweiss\Wechat\WechatException;
use luweiss\Wechat\WechatHelper;
use luweiss\Wechat\WechatHttpClient;
use luweiss\Wechat\WechatPay;

class WechatServicePay extends WechatPay
{
    public $sub_appid;
    public $sub_mch_id;

    /**
     * @param $api
     * @param $args
     * @return array
     * @throws WechatException
     */
    public function send($api, $args)
    {
        $args['sub_appid'] = !empty($args['sub_appid']) ? $args['sub_appid'] : $this->sub_appid;
        $args['sub_mch_id'] = !empty($args['sub_mch_id']) ? $args['sub_mch_id'] : $this->sub_mch_id;
        return parent::send($api, $args); // TODO: Change the autogenerated stub
    }

    /**
     * @param $api
     * @param $args
     * @return array
     * @throws WechatException
     */
    protected function sendWithPem($api, $args)
    {
        $args['sub_appid'] = !empty($args['sub_appid']) ? $args['sub_appid'] : $this->sub_appid;
        $args['sub_mch_id'] = !empty($args['sub_mch_id']) ? $args['sub_mch_id'] : $this->sub_mch_id;
        $args['nonce_str'] = !empty($args['nonce_str']) ? $args['nonce_str'] : md5(uniqid());
        $args['sign'] = $this->makeSign($args);
        $xml = WechatHelper::arrayToXml($args);
        $res = $this->getClient()
            ->setDataType(WechatHttpClient::DATA_TYPE_XML)
            ->setCertPemFile($this->certPemFile)
            ->setKeyPemFile($this->keyPemFile)
            ->post($api, $xml);
        return $this->getClientResult($res);
    }
}
